package utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * 閸ュ墽澧栧銉ュ徔缁紮绱欐担璺ㄦ暏娑撳啰澧版禍鎴濈摠閸屻劍婀囬崝鈽呯礆
 * @author www.shiyanlou.com
 *文件的上传和删除
 */
public class FileUtils {
    private static final String ACCESS_KEY = "hcS0jvE2a64ktlzNz0517G3Jdb81GZ-FRRLfTmUM";
    private static final String SECRET_KEY = "Qydx_Dvruv5e0z2g3xVjUlXRZuRDZ_ocT82Zjk6z";
    private static final String BUCKET_NAME = "flyaif-bucket";
    /**
     * 娑撳﹣绱堕崶鍓у閸掗绔烽悧娑楃隘鐎涙ê鍋�     * @param reader
     * @param fileName
     */
    public static void upload(InputStream in ,String fileName)
    {
      Configuration cfg = new Configuration(Zone.zone2());
      UploadManager uploadManager = new UploadManager(cfg);
      Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
      String upToken = auth.uploadToken(BUCKET_NAME);
      try{
        Response res = uploadManager.put(in, fileName, upToken, null, null);
        System.out.println(res);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    public static void main(String[] args) {
      File file = new File("e:\\gamersky_02origin_03_2016102920337E7.jpg");
      FileInputStream in;
      try {
        in = new FileInputStream(file);
        FileUtils.upload(in, "bz.jpg");
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    /**
     * 閸掔娀娅庢稉鍐娴滄垵鐡ㄩ崒銊ょ瑐閻ㄥ嫬娴橀悧锟�    * @param key
     */
    public static void delete(String key) {
      Configuration cfg = new Configuration(Zone.zone2());
      Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
      BucketManager bm = new BucketManager(auth, cfg);
      try {
        bm.delete(BUCKET_NAME, key);
      } catch (QiniuException e) {
        System.err.print(e.code());
        System.err.print(e.response.toString());
        
      }
    }
}